package jia.core.graphics;

import jia.game.GenericFlags;
import jia.core.Compass;
import org.apache.log4j.Logger;

import java.awt.image.BufferedImage;

// ====================================================

/**
 * @author Alec Lanter
 */
public class FirstPersonWallImage implements WallImage {
    
    private static final Logger log = Logger.getLogger(FirstPersonWallImage.class);
    private String imgResourceName;
    private BufferedImage[][] textures;
    
    // ====================================================
    /**
     */
    protected FirstPersonWallImage() {
        textures = new BufferedImage[3][];
        textures[0] = new BufferedImage[getMaxDistanceLevel() + 1];
        textures[1] = new BufferedImage[getMaxDistanceLevel() + 1];
        textures[2] = new BufferedImage[getMaxDistanceLevel() + 1];
    }
    
    // ====================================================
    /**
     * @see net.sf.jia.framework.graphics.WallImage#getTexture(net.sf.jia.framework.Compass,
     *net.sf.jia.framework.Compass,int)
     */
    public BufferedImage getTexture(Compass wallFacing,
        Compass viewportFacing,
        int distanceLevel) {
        Compass diffComp = Compass.add(Compass.differenceBetween(wallFacing, viewportFacing), Compass.SOUTH);
        if (GenericFlags.debugMessages) {
            log.debug("Texture request: Viewport is facing " + viewportFacing.getCardinalName() + ", and " + "I face " + wallFacing.getCardinalName() + ", so I need to hand an image facing " + diffComp.getCardinalName());
        }
        
        switch (diffComp.getDirectionalValue()) {
            case Compass.NORTH_VAL:
            case Compass.SOUTH_VAL:
                return textures[0][distanceLevel];
            case Compass.EAST_VAL:
                return textures[1][distanceLevel];
            case Compass.WEST_VAL:
                return textures[2][distanceLevel];
            default:
                return null;
        }
    }
    
    // ====================================================
    /**
     * @see net.sf.jia.framework.graphics.WallImage#getMaxDistanceLevel()
     */
    public int getMaxDistanceLevel() {
        return 2;
    }
    
    // ====================================================
    /**
     * @param direction
     * @param distanceLevel
     * @param img
     */
    protected void setTexture(Compass direction,
        int distanceLevel,
        BufferedImage img) {
        switch (direction.getDirectionalValue()) {
            case Compass.NORTH_VAL:
            case Compass.SOUTH_VAL:
                textures[0][distanceLevel] = img;
                break;
            case Compass.EAST_VAL:
                textures[1][distanceLevel] = img;
                break;
            case Compass.WEST_VAL:
                textures[2][distanceLevel] = img;
        }
    }
    
    //===============================================
    /**
     * @see net.sf.jia.framework.graphics.WallImage#getImageResourceName()
     */
    public String getImageResourceName() {
        return imgResourceName;
    }
    
    // ===============================================
    /**
     * @param imgResourceName The imgResourceName to set.
     */
    public void setImgResourceName(String imgResourceName) {
        this.imgResourceName = imgResourceName;
    }
}
